﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using DongDong.Common;
/// <summary>
///GZ 的摘要说明
/// </summary>
public partial class GZ
{
    public enum typeGZ { 辅导 = 1, 培训, 老师, 机构 }
    public static GZ GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.GZ.FirstOrDefault(row => row.ID == id);
        }
    }

    public static List<GZ> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.GZ.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.GZ.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<GZ>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.GZ.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.GZ.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }

    public static List<GZ> GetAllByConditon(int typeGZ, string AddTimeForm, string AddTimeTo, int GzUserID, int BgzID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.GZ
                        select t;
            if (typeGZ != 0)
                query = query.Where(row => row.Type == typeGZ);
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= DongDong.Common.Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= DongDong.Common.Misc.ConvertToDateTime(AddTimeTo));
            if (GzUserID != 0)
                query = query.Where(row => row.AddUserID == GzUserID);
            if (BgzID != 0)
                query = query.Where(row => row.GZID == BgzID);
            query = query.OrderByDescending(row => row.AddTime);
            return query.ToList();
        }
    }

    public static List<GZLS> GetAllGZLSByConditon(string AddTimeForm, string AddTimeTo, int GzUserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.GZ
                        join shiz in context.SZJBXX
                        on t.GZID equals shiz.ID
                        where t.Type == (int)typeGZ.老师 && shiz.Enable_Flag == 1 && t.AddUserID == GzUserID
                        join JG in context.JGXX
                        on shiz.SYJGID equals JG.ID
                        into grp1
                        from JF in grp1.DefaultIfEmpty()
                        select new GZLS
                        {
                            ID = t.ID,
                            TeachID = shiz.ID,
                            TeachNo = shiz.BianHao,
                            JGID = JF.ID,
                            JGName = JF.JFName ?? "暂无",
                            JGNo = JF.JGID,
                            Remark = t.Remark,
                            Sex = shiz.Sex,
                            SMRZStatus = shiz.SMRZStatus,
                            TeachDes = shiz.Des,
                            TeachName = (shiz.YSMC == 1 ? (shiz.Xing + shiz.Ming) : (shiz.Xing + "老师")),
                            TelPhone = shiz.Telphone,
                            AddTime = t.AddTime,
                            Des = shiz.Des
                        };
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= Misc.ConvertToDateTime(AddTimeTo));
            return query.OrderByDescending(row => row.AddTime).ToList();
        }
    }

    public class GZLS
    {
        public int ID { get; set; }
        public int TeachID { get; set; }
        public string TeachNo { get; set; }
        public string TeachName { get; set; }
        public string TeachDes { get; set; }
        public int JGID { get; set; }
        public string JGName { get; set; }
        public string JGNo { get; set; }
        public int Sex { get; set; }
        public int SMRZStatus { get; set; }
        public string TelPhone { get; set; }
        public string Remark { get; set; }
        public string Des { get; set; }
        public DateTime AddTime { get; set; }
    }

    public static List<GZPX> GetAllGZPXByConditon(string AddTimeForm, string AddTimeTo, int GzUserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.GZ
                        join PX in context.KCJBXX
                        on t.GZID equals PX.ID
                        where t.Type == (int)typeGZ.培训 && PX.Enable_Flag == 1 && t.AddUserID == GzUserID
                       
                        join LS in context.SZJBXX
                        on PX.LSID equals LS.ID
                        where LS.Enable_Flag == 1
                        join JG in context.JGXX
                        on PX.JGID equals JG.ID
                        into grp1
                        from JF in grp1.DefaultIfEmpty()
                        select new GZPX
                        {
                            ID = t.ID,
                            AddTime = t.AddTime,
                            JGID = JF.JGID,
                            JGName = JF.JFName ?? "暂无",
                            JGNo = JF.JGID,
                            KCJS = PX.KCJS,
                            PXID = PX.ID,
                            PXName = PX.KCMC,
                            PXNo = PX.KCBM,
                            Remark = t.Remark,
                            SHXY = PX.SHXY,
                            TeachID = LS.ID,
                            TeachName = (LS.YSMC == 1 ? (LS.Xing + LS.Ming) : (LS.Xing + "老师")),
                            TeachNo = LS.BianHao,
                            KMID = PX.KMID
                        };
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= Misc.ConvertToDateTime(AddTimeTo));
            return query.OrderByDescending(row => row.AddTime).ToList();
        }
    }

    public class GZPX
    {
        public int ID { get; set; }
        public int TeachID { get; set; }
        public string TeachNo { get; set; }
        public string TeachName { get; set; }
        public string JGID { get; set; }
        public string JGName { get; set; }
        public string JGNo { get; set; }
        public string KMID { get; set; }
        public int PXID { get; set; }
        public string PXName { get; set; }
        public string PXNo { get; set; }
        public string KCJS { get; set; }
        public string SHXY { get; set; }
        public string Remark { get; set; }
        public DateTime AddTime { get; set; }
    }

    public static List<GZJiaJ> GetAllGZJiaJByConditon(string AddTimeForm, string AddTimeTo, int GzUserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.GZ
                        join FD in context.JJFD
                        on t.GZID equals FD.ID
                        where t.Type == (int)typeGZ.辅导 && FD.Enable_Flag == 1 && t.AddUserID == GzUserID
                        join LS in context.SZJBXX
                        on FD.SZID equals LS.ID
                        where LS.Enable_Flag == 1
                        join JG in context.JGXX
                        on FD.JGID equals JG.ID
                        into grp1
                        from JF in grp1.DefaultIfEmpty()
                        select new GZJiaJ
                        {
                            ID = t.ID,
                            AddTime = t.AddTime,
                            JJID = JF.ID,
                            JJName = JF.JFName ?? "暂无",
                            JJNo = JF.JGID,
                            JSKM = FD.KMID,
                            JSQY = FD.ReginIDs,
                            TeachID = LS.ID,
                            TeachName = (LS.YSMC == 1 ? (LS.Xing + LS.Ming) : (LS.Xing + "老师")),
                            TeachNo = LS.BianHao,
                            Price = FD.XCYQ,
                            Remark = t.Remark,
                            TeachDes = LS.Des,
                            TelPhone = LS.Telphone,
                           
                        };
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= Misc.ConvertToDateTime(AddTimeTo));
            return query.OrderByDescending(row => row.AddTime).ToList();
        }
    }

    public class GZJiaJ
    {
        public int ID { get; set; }
        public int TeachID { get; set; }
        public string TeachNo { get; set; }
        public string TeachName { get; set; }
        public string TeachDes { get; set; }
        public int JJID { get; set; }
        public string JJName { get; set; }
        public string JJNo { get; set; }
        public string JSKM { get; set; }
        public string JSQY { get; set; }
        public string Price { get; set; }
        public string TelPhone { get; set; }
        public string Remark { get; set; }
       
        public DateTime AddTime { get; set; }
    }

    public static List<GZJJ> GetAllGZJJByConditon(string AddTimeForm, string AddTimeTo, int GzUserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.GZ
                        join JG in context.JGXX
                        on t.GZID equals JG.ID
                        where t.Type == (int)typeGZ.机构 && JG.Enable_Flag == 1 && t.AddUserID == GzUserID
                        select new GZJJ
                        {
                            ID = t.ID,
                            AddTime = t.AddTime,
                            Des = JG.Des,
                            JJID = JG.ID,
                            JJName = JG.JFName,
                            JJNo = JG.JGID,
                            Sort = JG.Sort,
                            Type = JG.Type,
                            Remark = t.Remark,
                            SMRZStatus = JG.SMRZStatus
                        };
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= Misc.ConvertToDateTime(AddTimeTo));
            return query.OrderByDescending(row => row.AddTime).ToList();
        }
    }
    public class GZJJ
    {
        public int ID { get; set; }
        public int JJID { get; set; }
        public string JJName { get; set; }
        public string JJNo { get; set; }
        public int SMRZStatus { get; set; }
        public string Des { get; set; }
        public int Sort { get; set; }
        public int Type { get; set; }
        public string Remark { get; set; }
        public DateTime AddTime { get; set; }
    }
}